//Source file: C:\\javier\\mario\\Java\\taladro\\puerto\\PuertoParalelo.java

package taladro.puerto;

import jni.IntPuertoParalelo;
import taladro.control.BlockingQueue;
import taladro.control.SystemTimer;

/**
 * <p>
 * </p>
 */
public class PuertoParalelo 
{
   private static IntPuertoParalelo intPuerto = null;
   private static BlockingQueue queue;
   private byte x = 0;
   /**
    * /////////////////////////////////////
    * attributes
    * <p>
    * Represents ...
    * </p>
    */
   private static PuertoParalelo instance = null;
   private PortSender sender = null;
   private String puerto;
   
   /**
    * end inPort        
    * <p>
    * Represents ...
    * </p>
    * @roseuid 3F341CC60197
    */
   protected PuertoParalelo() 
   {
       intPuerto = new IntPuertoParalelo();
       queue = new BlockingQueue();
       sender = new PortSender(queue);
        new Thread(sender).start();
   }
   
   public static PuertoParalelo getInstance(){
       if( instance == null)           
           instance = new PuertoParalelo();
       return instance;
   }
   
   /**
    * /////////////////////////////////////
    * operations
    * <p>
    * Does ...
    * </p><p>
    * @return a String with ...
    * </p>
    * @roseuid 3F341CC60165
    */
   public String getPuerto() 
   {        
        return puerto;    
   }
   
   /**
    * end getPuerto        
    * <p>
    * Does ...
    * </p><p>
    * </p><p>
    * @param _puerto ...
    * </p>
    * @roseuid 3F341CC6016F
    */
   public void setPuerto(String _puerto) 
   {
        puerto = _puerto;
        intPuerto.openPort( puerto );
   }
   
   public void setPuerto(String _puerto, int retardo) 
   {
        puerto = _puerto;
        intPuerto.openPort( puerto );
        intPuerto.setDelay(retardo );
   }
   
   /**
    * end setPuerto        
    * <p>
    * Does ...
    * </p><p>
    * </p><p>
    * @param dato ...
    * </p> JDGA MOVER
    * @roseuid 3F341CC6017A 
    */
   public void outPort(byte dato)
   {
       /*
       //System.out.print("\nSalida a puerto\n");
        //System.out.print( Integer.toHexString( (byte)(0x0F & dato) ) + " ");
       
        intPuerto.outPort( (byte)(0x0F & dato) );
        //System.out.print( Integer.toHexString( (byte)(0x0F & dato) ) + " ");
        SystemTimer.sleep(0.5);
       
        //System.out.println( Integer.toHexString(dato) );
        //System.out.println("... fin salida puerto" );
        intPuerto.outPort( dato );
        **/
       //System.out.println( "Por consumir " + Integer.toHexString(dato) + " " + System.currentTimeMillis() );
       Byte outPut = new Byte(dato);
       queue.push(outPut);
   }
   
   public void outPortDirect(byte output) {
       byte dato = output;
       //System.out.println("Consumiendo... " + Integer.toHexString(dato) + " " + System.currentTimeMillis() ); 
       intPuerto.outPort( (byte)(0x0F & dato) );
        //System.out.print( Integer.toHexString( (byte)(0x0F & dato) ) + " ");
        SystemTimer.sleep(7);
        //System.out.println("... fin salida puerto" );
        intPuerto.outPort( dato );
        //System.out.println("...Consumido " + Integer.toHexString(dato) +" " + System.currentTimeMillis() ); 
   }
   
   /**
    * end outPort        
    * <p>
    * Does ...
    * </p><p>
    * @return a byte with ...
    * </p>
    * @roseuid 3F341CC6018D
    */
   public byte inPort() 
   {        
        //your code here
       //return x++;
       return intPuerto.inPort();
   }
   
  class PortSender implements Runnable {
   private final BlockingQueue queue;
   PortSender (BlockingQueue q) { queue = q; }
   public void run() {
     try {
         System.out.println("Iniciado consumer... " + System.currentTimeMillis());
       while (true) { consume((Byte)queue.pop()); }
     } catch (InterruptedException ex) { ex.printStackTrace(); }
   }
   
   void consume(Byte output) {
       byte dato = output.byteValue();
       //System.out.println("Consumiendo... " + Integer.toHexString(dato) + " " + System.currentTimeMillis() ); 
       intPuerto.outPort( (byte)(0x0F & dato) );
        //System.out.print( Integer.toHexString( (byte)(0x0F & dato) ) + " ");
        SystemTimer.sleep(7);
        //System.out.println("... fin salida puerto" );
        intPuerto.outPort( dato );
        //System.out.println("...Consumido " + Integer.toHexString(dato) +" " + System.currentTimeMillis() ); 
   }
 }

   
}
/* 
PuertoParalelo.outPort(Byte){        
        // your code here
    }
 */
/**
 *  end PuertoParalelo
 */
